Notebook / example pipeline¶
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
import sys
import pypty
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle
from matplotlib.colors import LogNorm
from matplotlib.patches import Ellipse
from matplotlib import rcParams
plt.style.use('dark_background')
print(pypty.__version__)
import numpy as np
import matplotlib.pyplot as plt
import sys
import pypty
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle
from matplotlib.colors import LogNorm
from matplotlib.patches import Ellipse
from matplotlib import rcParams
plt.style.use('dark_background')
print(pypty.__version__)
2.1
Specify raw data and output path¶
In [2]:
Copied!
path="/home/anton/data/test_folder/"
name="mops"
path_h5=path+ name+".h5"
path_json=""
path_raw=path+name+".npy"
output_folder="/home/anton/data/test_folder/test_mops_2/"
path="/home/anton/data/test_folder/"
name="mops"
path_h5=path+ name+".h5"
path_json=""
path_raw=path+name+".npy"
output_folder="/home/anton/data/test_folder/test_mops_2/"
Create h5 data¶
In [3]:
Copied!
pypty.initialize.create_pypty_data(path_raw, path_h5, swap_axes=False,
flip_ky=0,flip_kx=0, comcalc_len=200*200,
comx=0, comy=0, bin=1, crop_left=None,
crop_right=None, crop_top=None, crop_bottom=None, normalize=False, exist_ok=1)
pypty.initialize.create_pypty_data(path_raw, path_h5, swap_axes=False,
flip_ky=0,flip_kx=0, comcalc_len=200*200,
comx=0, comy=0, bin=1, crop_left=None,
crop_right=None, crop_top=None, crop_bottom=None, normalize=False, exist_ok=1)
****************************************************************************** ************************ Creating an .h5 File ******************************** ****************************************************************************** .h5 File exists!
Parameters¶
In [5]:
Copied!
experimental_params={
'output_folder': output_folder,
'path_json': path_json,
'data_path': path_h5,
'conv_semiangle_mrad': 4,
'scan_size': [28,28],
'scan_step_A': 20,
'fov_nm': 2*27,
'acc_voltage': 200, ## kV
'PLRotation_deg': 0, # deg
'bright_threshold': 0.2,
'plot': True,
'print_flag': 1,
'data_pad': None,
'sample_name': 'nano_mops'
}
pypty_params={
'update_obj': 1,
'update_probe': 1,
'hist_length': 10,
'optimizer': "bfgs",
'update_step_bfgs': lambda x: 1e-2 if x==0 else 1,
'n_parallel_meas': 1,
'min_step': 1e-30,
'optimism': 3,
'reduce_factor': 1/10,
'wolfe_c1_constant': 1e-1,
'dynamically_resize_yx_object': 5,
'aberrations': [-2e4,0,0], ## Angstrom # 200A
'wolfe_c2_constant': 0.9,
'update_extra_cut': 0.02,
'save_inter_checkpoints': True,
'save_checkpoints_every_epoch': False,
'algorithm': 'lsq_sqrt',
'use_full_FOV': True,
'epoch_max': 100,
'load_one_by_one': False,
'preload_to_cpu': False,
'smart_memory': False,
'force_pad': True,
'default_dtype': 'double',
'damping_cutoff_multislice': 0.66,
}
pypty_params=pypty.initialize.append_exp_params(experimental_params, pypty_params)
experimental_params={
'output_folder': output_folder,
'path_json': path_json,
'data_path': path_h5,
'conv_semiangle_mrad': 4,
'scan_size': [28,28],
'scan_step_A': 20,
'fov_nm': 2*27,
'acc_voltage': 200, ## kV
'PLRotation_deg': 0, # deg
'bright_threshold': 0.2,
'plot': True,
'print_flag': 1,
'data_pad': None,
'sample_name': 'nano_mops'
}
pypty_params={
'update_obj': 1,
'update_probe': 1,
'hist_length': 10,
'optimizer': "bfgs",
'update_step_bfgs': lambda x: 1e-2 if x==0 else 1,
'n_parallel_meas': 1,
'min_step': 1e-30,
'optimism': 3,
'reduce_factor': 1/10,
'wolfe_c1_constant': 1e-1,
'dynamically_resize_yx_object': 5,
'aberrations': [-2e4,0,0], ## Angstrom # 200A
'wolfe_c2_constant': 0.9,
'update_extra_cut': 0.02,
'save_inter_checkpoints': True,
'save_checkpoints_every_epoch': False,
'algorithm': 'lsq_sqrt',
'use_full_FOV': True,
'epoch_max': 100,
'load_one_by_one': False,
'preload_to_cpu': False,
'smart_memory': False,
'force_pad': True,
'default_dtype': 'double',
'damping_cutoff_multislice': 0.66,
}
pypty_params=pypty.initialize.append_exp_params(experimental_params, pypty_params)
****************************************************************************** ******** Attaching the experimental parameters to your PyPty preset. ********* ****************************************************************************** json is not provided!
Radius of bright field is 62.52 px pixel size in A: 1.531e+00 Pixel size after padding: 1.02e+00 Å
tcBF¶
In [6]:
Copied!
pypty_params=pypty.tcbf.run_tcbf_alignment(pypty_params,
aberrations=[-1.5e4,0,0],
binning_for_fit=np.tile([7],30),
refine_box_dim=10, upsample=3,
reference_type="bf", optimize_angle=False,
cancel_large_shifts=0.9,
subscan_region=None, interpolate_scan_factor=1,
cross_corr_type="phase"
)
pypty_params=pypty.tcbf.run_tcbf_alignment(pypty_params,
aberrations=[-1.5e4,0,0],
binning_for_fit=np.tile([7],30),
refine_box_dim=10, upsample=3,
reference_type="bf", optimize_angle=False,
cancel_large_shifts=0.9,
subscan_region=None, interpolate_scan_factor=1,
cross_corr_type="phase"
)
****************************************************************************** ************************ Running the tcBF alignment ************************** ****************************************************************************** Initial aberrations: C10 -1.50e+04 Å, C12a 0.00e+00 Å, C12b 0.00e+00 Åshape of data: (784, 256, 256) scan size: [28 28] Initializing the abberation fit! ---> Starting iteration 1/30 of the CTF fit, this binning is 7 Binning the data by 7 FFT of binned data is done!
/home/anton/pypty/pypty/tcbf.py:304: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache() ## free the memory
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.09e+04 A, C12a 2.92e+02 A, C12b 6.52e+02 A ---> Starting iteration 2/30 of the CTF fit, this binning is 7 Using results of previous binning
/home/anton/pypty/pypty/tcbf.py:588: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache()
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 2.79e+00 A, C12b 1.21e+00 A ---> Starting iteration 3/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 4/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 5/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 6/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 7/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 8/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 9/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 10/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 11/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 12/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 13/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 14/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 15/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 16/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 17/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 18/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 19/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 20/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 21/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 22/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 23/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 24/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 25/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 26/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 27/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 28/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 29/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A ---> Starting iteration 30/30 of the CTF fit, this binning is 7 Using results of previous binning
Found matching shifts for 258/287 pixels. CTF fitted successfully: True.
Fitted aberrations: C10 -2.23e+04 A, C12a 7.74e-01 A, C12b -1.89e+00 A Final CTF Fit done!
/home/anton/pypty/pypty/tcbf.py:625: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache()
In [7]:
Copied!
im, pix=pypty.tcbf.upsampled_tcbf(pypty_params,
round_shifts=True,
upsample=5)
plt.imshow(im, cmap="gray")
plt.title("Upsampled tcBF")
plt.colorbar()
plt.show()
im, pix=pypty.tcbf.upsampled_tcbf(pypty_params,
round_shifts=True,
upsample=5)
plt.imshow(im, cmap="gray")
plt.title("Upsampled tcBF")
plt.colorbar()
plt.show()
****************************************************************************** ************************ Creating upsampled tcBF Image *********************** ****************************************************************************** 6.40e-05 rad per pixel your final pixel size will be 4.00 Å final shape of image will be: (240, 240)
100%|██████████| 12281/12281 [00:02<00:00, 4569.58it/s] /home/anton/pypty/pypty/tcbf.py:856: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. xp.fft.config.clear_plan_cache()
Direct Ptychography¶
In [8]:
Copied!
o=pypty.direct.wdd(pypty_params)
plt.imshow(np.angle(o), cmap="gray")
plt.title("WDD Phase")
plt.colorbar()
plt.axis("off")
plt.show()
o=pypty.direct.wdd(pypty_params)
plt.imshow(np.angle(o), cmap="gray")
plt.title("WDD Phase")
plt.colorbar()
plt.axis("off")
plt.show()
/home/anton/pypty/pypty/direct.py:46: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache()
Provided aberrations: C10: -2.23e+04 Å; C12a: 7.74e-01 Å; C12b: -1.89e+00 Å;
/home/anton/pypty/pypty/direct.py:170: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache() 100%|██████████| 256/256 [00:06<00:00, 39.32it/s] /home/anton/pypty/pypty/direct.py:182: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache()
-->1st FFT done
100%|██████████| 28/28 [00:00<00:00, 51.31it/s] /home/anton/pypty/pypty/direct.py:201: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache()
-->2nd FFT done (array(14), array(14))
Iterative Ptychography¶
In [9]:
Copied!
pypty.iterative.run(pypty_params)
pypty.iterative.run(pypty_params)
***************************************************** ************************* ************************ Starting PyPty Reconstruction *********************** ****************************************************************************** Path to the dataset: /home/anton/data/test_folder/mops.h5 Saving the results in /home/anton/data/test_folder/test_mops_2/ Saving the parameters in /home/anton/data/test_folder/test_mops_2/params.pkl The log file will be saved as /home/anton/data/test_folder/test_mops_2/loss.csv ****************************************************************************** ****************************************************************************** ****************************************************************************** The probe was generated based on the mean pattern! WARNING! Range of specified scan positions was larger than the shape of the object, adding ones to the right and/or bottom. New shape of the object is 913 px in y, 913 px in x, 1 slice(-s) , 1 mode(-s) !!!!! Provided aberrations: C10: -2.23e+04 Å; C12a: 7.74e-01 Å; C12b: -1.89e+00 Å; ---------> Time: 0: 0:17. Epoch 99. Using lsq_sqrt error metric with bfgs-10 optimzer. Loss: 5.07e+01. SSE: 7.34e+01. Updating object, probe; Done :)
/home/anton/pypty/pypty/iterative.py:336: FutureWarning: cupy.fft.cache.clear_plan_cache is experimental. The interface can change in the future. cp.fft.config.clear_plan_cache()
Visualisation¶
In [10]:
Copied!
output_folder="/home/anton/data/test_folder/test_mops_2/"
o,p,g=np.load(output_folder+"co.npy"), np.load(output_folder+"cp.npy"), np.load(output_folder+"cg.npy")
phase=np.angle(o[:,:,0,0])
plt.imshow(phase, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.show()
pypty.vaa.plot_modes(p)
figs=pypty.vaa.outputlog_plots(output_folder+"loss.csv", 10, True)
output_folder="/home/anton/data/test_folder/test_mops_2/"
o,p,g=np.load(output_folder+"co.npy"), np.load(output_folder+"cp.npy"), np.load(output_folder+"cg.npy")
phase=np.angle(o[:,:,0,0])
plt.imshow(phase, cmap="gray")
plt.colorbar()
plt.axis("off")
plt.show()
pypty.vaa.plot_modes(p)
figs=pypty.vaa.outputlog_plots(output_folder+"loss.csv", 10, True)
In [ ]:
Copied!